package com.chinasoftosg.sicap.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5 encrypt
 * 
 * @author ZhangBo
 * 
 */
public class MD5
{
    
    private static final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    
    public static String encrypt(String pwd)
    {
        byte[] bs = pwd.getBytes();
        String rtn = null;
        
        try
        {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(bs);
            byte tmp[] = md5.digest();
            
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < 16; i++)
            {
                byte b = tmp[i];
                buf.append(HEX_DIGITS[b >>> 4 & 0xf]);
                buf.append(HEX_DIGITS[b & 0xf]);
            }
            rtn = buf.toString();
            
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        
        return rtn;
    }
    
    public static void main(String[] args)
    {
        System.out.println(MD5.encrypt("umb_2012"));
    }
}
